﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using System.Text;
using Model;

namespace PMS.Web
{
    /// <summary>
    /// ProjectMaintian 的摘要说明
    /// </summary>
    public class ProjectMaintian : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strAction = context.Request["action"];
            string strReturn = "";
            if (!string.IsNullOrEmpty(strAction))
            {
                System.Reflection.MethodInfo minfo = this.GetType().GetMethod(strAction);
                if (minfo != null)
                {
                    object o = minfo.Invoke(this, new object[] { context });
                    if (o != null)
                        strReturn = o.ToString();
                }
            }
            context.Response.Write(strReturn);
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string DeleteProject(HttpContext context)
        {
            string projectID = context.Request["projectID"];
            int id = 0;
            if (!string.IsNullOrEmpty(projectID))
                id = Convert.ToInt32(projectID);
            StringBuilder strHtml = new StringBuilder();
            BLL.PMS_ProjectBLL bll = new PMS_ProjectBLL();
            if (bll.Delete(id))
            {
                return "0";
            }
            else
            {
                return "-1";
            }
        }

        /// <summary>
        /// 查询项目列表
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public string GetProjectList(HttpContext context)
        {
            string projectID = context.Request["projectID"];
            string jdeID = context.Request["jdeID"];
            string startDate = context.Request["startDate"];
            string endDate = context.Request["endDate"];
            string roleID = context.Request["roleID"];
            string userID = context.Request["userID"];
            PMS_ProjectQuery query = new PMS_ProjectQuery();
            if (!string.IsNullOrEmpty(projectID))
                query.projectID = Convert.ToInt32(projectID);
            if (!string.IsNullOrEmpty(startDate))
                query.startDate = Convert.ToDateTime(startDate);
            if (!string.IsNullOrEmpty(endDate))
                query.endDate = Convert.ToDateTime(endDate);
            if (!string.IsNullOrEmpty(roleID))
                query.roleID = Convert.ToInt32(roleID);
            if (!string.IsNullOrEmpty(userID))
                query.userID = Convert.ToInt32(userID);
            query.projectType = "";

            BLL.PMS_ProjectBLL bll = new BLL.PMS_ProjectBLL();
            PMS_UserBLL ubll = new PMS_UserBLL();
            PMS_User user = ubll.GetModel(Convert.ToInt32(userID));

            string display = "style=\"cursor:pointer;display:none;\"";
            if (user.role == "管理员")
            {
                display = "style=\"cursor:pointer;\"";
            }

            List<PMS_Project> list = bll.GetList(query);

            StringBuilder strHtml = new StringBuilder();
            strHtml.Append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><th class=\"GridHeader\" width=\"8%\" align=\"center\">工程编号</th><th class=\"GridHeader\" width=\"8%\">JDE 号</th><th class=\"GridHeader\">项目名称</th><th class=\"GridHeader\" width=\"9%\">开工日期</th><th class=\"GridHeader\" width=\"9%\">完工日期</th><th " + display + " class=\"GridHeader\" width=\"5%\">编辑</th><th " + display + " class=\"GridHeader\" width=\"5%\">删除</th><th class=\"GridHeader\" width=\"5%\">详细</th><th class=\"GridHeader\" width=\"8%\">项目进展</th><th class=\"GridHeader\" width=\"8%\">维护保养</th><th class=\"GridHeader\" width=\"8%\">维修更换</th><th class=\"GridHeader\" width=\"8%\">财务状态</th></tr>");
            foreach (PMS_Project m in list)
            {
                strHtml.Append("<tr><td class=\"GridTableTD\" align=\"center\">" + m.projectID + "</td><td class=\"GridTableTD\">" + m.JDEID + "</td><td class=\"GridTableTD\">" + m.projectName + "</td><td class=\"GridTableTD center\">" + m.startDate.ToString("yyyy-MM-dd") + "</td><td class=\"GridTableTD center\">" + m.endDate.ToString("yyyy-MM-dd") + "</td><td class=\"GridTableTD center\" " + display + " onclick=\"editProject(" + m.id + ")\">编辑</td><td class=\"GridTableTD center\" " + display + " onclick=\"deleteProject(" + m.id + ")\">删除</td><td class=\"GridTableTD center\" style=\"cursor:pointer;\" onclick=\"desProject(" + m.id + ")\">查看</td><td class=\"GridTableTD center\" style=\"cursor:pointer;\" onclick=\"desProjectEvelve(" + m.id + ")\">查看</td><td class=\"GridTableTD center\" style=\"cursor:pointer;\" onclick=\"desProjectMaintain(" + m.id + ")\">查看</td><td class=\"GridTableTD center\" style=\"cursor:pointer;\" onclick=\"desProjectRenewal(" + m.id + ")\">查看</td><td class=\"GridTableTD center\" style=\"cursor:pointer;\" onclick=\"desProjectState(" + m.id + ")\">查看</td></tr>");
            }
            if (list == null || list.Count <= 0)
            {
                strHtml.Append("<tr><td colspan=\"12\" class=\"GridTableTD\" align=\"center\">没有数据</td></tr>");
            }
            strHtml.Append("</table>");
            return strHtml.ToString();
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}